/**
 * Created by Administrator on 15-10-8.
 */
var pager = 1;
//function getTongYou(){
//    var geolocation = new BMap.Geolocation();
//    geolocation.getCurrentPosition(function(r) {
//        var storage = window.localStorage;
//        storage.setItem("lng", r.point.lng);
//        storage.setItem("lat", r.point.lat);
//    });

    //var param={};
    //var keyword=$("#search").val();
    //if(keyword){
    //   // param.nickName=keyword
    //    param.toAddress=keyword;
    //}
    //console.log(2);
    var url = getUrl();
    //var userid=getUserId();
    var sex=$.getUrlParam("sex");
    var startTimeString=$.getUrlParam("startTimeString");
    var goAddress=$.getUrlParam("goAddress");
    var targetSpots=$.getUrlParam("targetSpots");
    var rentType=$.getUrlParam("rentType");
    var brightSpot=$.getUrlParam("brightSpot");
    $.jsonp({
        url: 'http://'+url+'/api/sameway/sameway_list',
        data:{sex:sex,startTimeString:startTimeString,goAddress:goAddress,targetSpots:targetSpots,rentType:rentType,brightSpot:brightSpot,pager:pager},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            //console.log(data);
            $("#tongyoulist").html("");
            //$("#enterIdsNumber").text(data.arrays.length);
            var html="";
            $.each(data.arrays,function(index,ele){
                //if(ele.endTime <ele.startTime){
                //    showToast("开始时间不能小于结束时间~");
                //    return;
                //}
                var day=new Date(ele.endTime*1000)-new Date(ele.startTime*1000);
                //var day= parseInt(Math.abs((ele.endTime - ele.startTime))/(1000*60*60*24))+1;
                var day= parseInt(day/(1000*60*60*24))+1;
                //console.log(ele.endTime,ele.startTime,day);
                var unixTimestamp = new Date(ele.startTime*1000)
                commontime = unixTimestamp.toLocaleString();
                var unixTimestamp = new Date(ele.endTime*1000)
                commonTime = unixTimestamp.toLocaleString();


                //commontime =  commontime.substring(0,  commontime.lastIndexOf(".")).replace(/-/g, "/ ");
                //commonTime =  commonTime.substring(0,  commonTime.lastIndexOf(".")).replace(/-/g, "/ ");
                ////去掉毫秒 把-替换成/ 如果不替换转成时间戳类型火狐会出问题
                //var date1 = Date.parse( commontime);
                //var date2 = Date.parse( commonTime);
                //var day= (date2 - date1) / (60*60*1000*24)
                //var day =  commonTime-commontime;
                var id =ele.id;
                var sex=ele.sex;
                var sexstr='icons-sex-female';
                var sexbgstr='sex sex-female';
                if(sex==1){
                    sexstr="icons-sex-male";
                    sexbgstr='sex sex-male';
                }
                var jumpurl = "jumptoLogin("+"'tongyou_detail.html?id="+ele.id+"','tongyou_detail.html?id="+ele.id+"')";
                html += '<div onclick="'+jumpurl+'">' +
                '<div class="ui-source-pic">' +
                '<img src="' + ele.photos[0] + '" height="300"class="round-pic" alt=""> ' +
                '<div class="ui-source-pic-detail">' +
                '<div class="ui-source-pic-avatar">' +
                    //'<a href="tade.html?id=' + ele.id + '">' +
                '<img src="' + ele.photo+ '" width="35" height="35" alt=""></a>' +
                '</div>' +
                '<div class="ui-source-pic-content">' +
                '<div class="clearfix">' +
                '<div class="ui-source-pic-name">' +
                '' + ele.nickName + '<span class="' + sexbgstr + '" ><i class="' + sexstr + '"></i>' + ele.age + '</span>' +
                '</div>' +
                '<div class="ui-source-pic-number">' +
                '' + ele.enterIdsNumber + '人已报名' +
                '</div>' +
                '</div>' +
                '<div class="ui-source-pic-date">' +
                '' +getTime(ele.startTime)+ ' / ' + day+ '天 ' + ele.goAddress + '，' + ele.targetSpots + '' +
                '</div>' +
                ' </div>' +
                '  </div>' +
                '  </div>' +
                ' </div> ';
                $("#tongyoulist").html(html);
                if(data.arrays.length<5){
                    $(".load-more").text("");
                    return;
                }
            })

        },

        error: function (xOptions, textStatus) {

        }
    });
    $(".load-more").on("click",function() {
        pager = pager + 1;
        $.jsonp({
            url: 'http://'+url+'/api/sameway/sameway_list',
            data:{sex:sex,startTimeString:startTimeString,goAddress:goAddress,targetSpots:targetSpots,rentType:rentType,brightSpot:brightSpot,pager:pager},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                //console.log(data);
                $("#tongyoulist").html("");
                //$("#enterIdsNumber").text(data.arrays.length);
                var html="";
                $.each(data.arrays,function(index,ele){
                    //if(ele.endTime <ele.startTime){
                    //    showToast("开始时间不能小于结束时间~");
                    //    return;
                    //}
                    var day=new Date(ele.endTime*1000)-new Date(ele.startTime*1000);
                    //var day= parseInt(Math.abs((ele.endTime - ele.startTime))/(1000*60*60*24))+1;
                    var day= parseInt(day/(1000*60*60*24))+1;
                    var unixTimestamp = new Date(ele.startTime*1000)
                    commontime = unixTimestamp.toLocaleString();
                    var test = new Date(ele.endTime*1000)
                    var testt = test.toLocaleString();
                    console.log(commontime,testt,day);

                    var unixTimestamp = new Date(ele.endTime*1000)
                    commonTime = unixTimestamp.toLocaleString();
                    //var day =  commonTime-commontime;
                    var id =ele.id;
                    var sex=ele.sex;
                    var sexstr='icons-sex-female';
                    var sexbgstr='sex sex-female';
                    if(sex==1){
                        sexstr="icons-sex-male";
                        sexbgstr='sex sex-male';
                    }

                    var jumpurl = "jumptoLogin(" + "'tongyou_detail.html?id=" + ele.id + "','tongyou_detail.html?id=" + ele.id + "')";
                    html += '<div onclick="' + jumpurl + '">' +
                    '<div class="ui-source-pic">' +
                    '<img src="' + ele.photos[0] + '" height="300"class="round-pic" alt=""> ' +
                    '<div class="ui-source-pic-detail">' +
                    '<div class="ui-source-pic-avatar">' +
                        //'<a href="tade.html?id=' + ele.id + '">' +
                    '<img src="' + ele.photo + '" width="35" height="35" alt=""></a>' +
                    '</div>' +
                    '<div class="ui-source-pic-content">' +
                    '<div class="clearfix">' +
                    '<div class="ui-source-pic-name">' +
                    '' + ele.nickName + '<span class="' + sexbgstr + '" ><i class="' + sexstr + '"></i>' + ele.age + '</span>' +
                    '</div>' +
                    '<div class="ui-source-pic-number">' +
                    '' + ele.enterIdsNumber + '人已报名' +
                    '</div>' +
                    '</div>' +
                    '<div class="ui-source-pic-date">' +
                    '' + getTime(ele.startTime) + ' / ' + day + '天 ' + ele.goAddress + '，' + ele.targetSpots + '' +
                    '</div>' +
                    ' </div>' +
                    '  </div>' +
                    '  </div>' +
                    ' </div> ';
                    $("#tongyoulist").html(html);
                    if (data.arrays.length <5) {
                        $(".load-more").text("");
                        return;
                    }

                })
            },

            error: function (xOptions, textStatus) {

            }
        });
    });



window.onload=function(){
    //getTongYou();
    ////alert($.getUrlParam("id"));
    //
    //$(".ui-icon-search").on('click',function() {
    //    //console.log(1);
    //    getTongYou();
    //});

}



//function onBackKeyDown() {
//    jumpbyback(0,"");
//}
function getTime(t){
    var nt = t*1000;
    var n = new Date(nt);
    return formatDate(n);
}
function formatDate(now)   {
    var   year=now.getFullYear();
    var   month=now.getMonth()+1;
    var   date=now.getDate();
    return   year+"-"+(month>=10?month:"0"+month)+"-"+(date>=10?date:"0"+date);
}
//function GetDay() {
//    var arrDate, objDate1, objDate2, intDays;
//
//    objDate1 = new Date();
//    objDate2 = new Date();
//
//    arrDate = $("#开始日期").val().split("-");
//    objDate1.setFullYear(arrDate[0], arrDate[1], arrDate[2]);
//
//    arrDate = $("#结束日期").val().split("-");
//    objDate2.setFullYear(arrDate[0], arrDate[1], arrDate[2]);
//
//    intDays = parseInt(Math.abs(objDate1 - objDate2) / 1000 / 60 / 60 / 24);
//
//    $("#天数").val(intDays + 1);
//}
function jump(url){
    var spotsId = $.getUrlParam('id');
    location.href=url+'?id='+spotsId
}